#!/usr/bin/env python3

#  Copyright © 2015 SpaghettiToastBook
#
#  This file is part of Dimension BX2.1.
#
#  Dimension BX2.1 is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Dimension BX2.1 is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with Dimension BX2.1. If not, see <http://www.gnu.org/licenses/>.

import cocos
import pyglet
from pyglet.gl import *

"""Module for miscellaneous constants/objects used in multiple scenes."""

__all__ = ['loader', 'window', 'window_width', 'window_height', 'bullet_sound',
           'explosion_sound', 'fps', 'font_name', 'font_size', 'font_color']

glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

# Patch for gdkpixbuf2 segfaulting (pyglet issue 635)
old_image_load = pyglet.image.load
def new_image_load(filename, file=None, decoder=None):
    if decoder is None:
        decoder = pyglet.image.codecs.pil.PILImageDecoder()
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    return old_image_load(filename, file=file, decoder=decoder)
pyglet.image.load = new_image_load

old_font_load = pyglet.font.load
def new_font_load(name=None, size=None, bold=False, italic=False, dpi=None):
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    return old_font_load(name, size, bold, italic, dpi)
pyglet.font.load = new_font_load

# Resource loader
loader = pyglet.resource.Loader(path=['Resources/Graphics',
                                      'Resources/Sound Effects'])

# Window and properties
window_title = 'Dimension BX2.1'
window_width = 640
window_height = 480
window = cocos.director.director.init(width=window_width, height=window_height,
                                      caption=window_title, resizable=True)

# FPS and audio driver settings
fps = 60
pyglet.options['audio'] = ('alsa', 'openal', 'silent')
pyglet.clock.set_fps_limit(fps)

# Sound effects
bullet_sound = loader.media('Shoot.ogg', streaming=False)
explosion_sound = loader.media('Boom.ogg', streaming=False)

# Font stuff
font_name = 'Press Start 2P'
loader.add_font('PressStart2P.ttf')
font_size = 12
font_color = (0x17, 0xF0, 0x18, 0xFF)
